package me.luraframework.auth.security;

import lombok.RequiredArgsConstructor;
import me.luraframework.auth.security.dto.JwtUserDto;
import me.luraframework.auth.system.service.RoleService;
import me.luraframework.auth.system.service.UserService;
import me.luraframework.auth.system.service.dto.UserDto;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class DefaultUserDetailsService implements UserDetailsService {

    private final UserService userService;
    private final RoleService roleService;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserDto user = userService.findByName(username);
        return   new JwtUserDto(
                user,
                roleService.mapToGrantedAuthorities(user)
        );


    }
}
